var model = require('./model.js');
var fs =  require('fs');
var path = require('path');
var url = require('url');
var checkLogin = require('./checkLogin.js');
var cookieParse = require('./cookieParse.js');


//未登录时的界面,如果想从该页面查看文章就会跳转到登录页面
module.exports.blogs = function (req, res) {
    model.getAll(function(result) {
        var viewPath = path.join('./public', 'blogs.html');
        var viewData = { posts: result, admin: result};
        res.render(viewPath, viewData);
    });
}
//登陆功能
module.exports.login = function (req, res) {
    req.pcp(function (postData) {
        var username = postData.username;
        var password = postData.password;

        model.checkAdmin(username, password, function (result){
            if (result.length == 0) {
                res.redirect('/');
            }else{
                var sessionId = Date.now() + '_' + Math.random();
                res.setHeader('Set-Cookie', 'sessionId=' + sessionId);
                session[sessionId] = {
                    id : result[0].id,
                    username: result[0].username
                }
                res.redirect('/blogslogin');
            }
        })
    });
}
//注册功能
module.exports.register = function (req, res) {
    var viewPath = path.join('./public', 'register.html');
    var viewData = {};
    res.render(viewPath, viewData);
}
module.exports.charu = function (req, res) {
    req.pcp(function(postData) {
        var admin = {
            username : postData.username,
            password : postData.password,
            gender : postData.gender,
            lables : postData.lables
        };
        model.charu(admin, function(data) {
            res.redirect('/login');
        });
    });
}
//登录成功后进入的主页面，从此处跳转不需要重新登录
module.exports.blogslogin = function(req, res) {
    if(checkLogin(req)) {
        model.getAll(function(result) {
            var viewPath = path.join('./public', 'blogs.html');
            var viewData = { posts: result, sessionName:res-session.username, admin: req.session};
            res.render(viewPath, viewData);
        });
    }else{
        res.redirect('/');
    }
}
//搜索功能，用于点击姓名查询所有发表的帖子
module.exports.search = function (req, res) {
    if(checkLogin(req)) {
    var searchBy = req.gcp().searchBy;
    var keywords = req.gcp().keywords;
    var filedName = searchBy;
    var value = keywords;
    var username = session.username;

    model.getByField(filedName, value, function(result) {
        var viewPath = path.join('./public', 'geren.html');
        var viewData = { posts: result, post:result, admin: result };
        res.render(viewPath, viewData);
    });}
    else{
        res.redirect('/');
    }
}

//实现点击名称开始搜索该名称旗下的全部帖子
module.exports.find = function (req, res) {
    if(checkLogin(req)) {
    var findBy = req.gcp().findBy;
    var keywords = req.gcp().keywords;
    var filedName = findBy;
    var value = keywords;

    model.getQ(filedName, value, function(result) {
        var viewPath = path.join('./public', 'reply.html');
        var viewData = { posts: result, replys:result,reply:result, post:result, admin: result };
        res.render(viewPath, viewData);
    });}
    else{
        res.redirect('/');
    }
}

//发表文章，publication跳转至发表界面，insert进行插入post表中
module.exports.publication = function(req, res) {
    if(checkLogin(req)) {
        model.getAll(function(result) {
            var viewPath = path.join('./public', 'post.html');
            var viewData = { posts: result, admin: req.session };
            res.render(viewPath, viewData);
        });
    }else{
        res.redirect('/');
    }
}
//发表文章
module.exports.insert = function (req, res) {
    req.pcp(function(postData) {
        var post = {
            username : postData.username,
            time : postData.time,
            title : postData.title,
            conments : postData.conments
        };
        model.insert(post, function(data) {
            res.redirect('/blogslogin');
        });
    });
}
//查看文章详情功能
module.exports.modify = function (req, res) {
    
    var id = req.gcp().id;
    if(checkLogin(req)) {
    model.getByField('id', id, function(result) {
        var viewPath = path.join('./public', 'blog.html');
        var viewData = { posts: result, post: result,admin:req-session };
        res.render(viewPath, viewData);
    });
}else{
res.redirect('/');
}
}
//修改文章
module.exports.revise = function (req, res) {
    var id = req.gcp().id;//获取需要修改的文章id
    if(checkLogin(req)) {
    model.getByField('id', id, function(result) {//根据获取的id进行调用getByFielf函数打开要修改的文章
        var viewPath = path.join('./public', 'revise.html');
        var viewData = { posts: result, post: result,admin:req-session };
        res.render(viewPath, viewData);
    });
}else{
        res.redirect('/');
}
}
//更新文章
module.exports.update = function(req, res) {//调用update函数将对文章的修改保存进入数据库中
    var id = req.gcp().id;
req.pcp(function (postData){
    var post = {
        id: id,
        username : postData.username,
        gender : postData.gender,
        time : postData.time,
        title : postData.title,
        conments : postData.conments
    };
    model.updateById(post, function() {
        res.redirect('/blogslogin');
    });
});
};
//删除所发表的文章
module.exports.delete = function (req, res) {
    var id = req.gcp().id;
    model.deleById(id, function () {
        res.redirect('/blogslogin');
    });
}
//发表评论功能界面
module.exports.leave = function (req, res) {
    req.pcp(function(postData) {
        var reply = {
            username : postData.username,
            time : postData.time,
            tiezi : postData.tiezi,
            title : postData.title,
        };
        model.leave(reply, function(data) {
            res.redirect('/blogslogin');
        });
    });
}
//主页面，当不输入任何后缀名时默认进入此界面
module.exports.home = function(req, res){
    var viewPath = path.join('./public', 'login.html');
    var viewData = {}; 
    res.render(viewPath, viewData);
}

//退出登录
module.exports.logout = function(req, res) {
    var cookie = cookieParse(req);
    delete session[cookie.sessionId];
    res.redirect('/');
}
